﻿using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;

namespace SocialPhotosService
{
    public class WcfOData : DataService<SocialPhotosEntities>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            // Examples:
            // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);
            // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);

            config.SetServiceOperationAccessRule(
                   "*", ServiceOperationRights.All);

            config.SetEntitySetAccessRule("*",
               EntitySetRights.ReadMultiple |
               EntitySetRights.ReadSingle |
               EntitySetRights.AllWrite);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }

        [WebGet]
        public byte[] GetImageBinaryID(int photoID)
        {
            var photo = (from d in this.CurrentDataSource.Photos
                         where d.Id == photoID
                         select d).FirstOrDefault();
            if(photo is Photo)
                return ((Photo)photo).Image;
            else
                return new byte[]{};
        }

       

    }
}
